package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataCollectionTypeDTO;
import com.ctshk.rpc.system.entity.MainDataCollectionType;
import com.ctshk.rpc.system.enums.ISCode;
import com.ctshk.rpc.system.mapper.MainDataCollectionTypeMapper;
import com.ctshk.rpc.system.req.MainDataCollectionTypeDetailReq;
import com.ctshk.rpc.system.req.MainDataCollectionTypeListReq;
import com.ctshk.rpc.system.service.IMainDataCollectionTypeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 收款类型 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class MainDataCollectionTypeServiceImpl extends ServiceImpl<MainDataCollectionTypeMapper, MainDataCollectionType> implements IMainDataCollectionTypeService {

    @Autowired
    private MainDataCollectionTypeMapper mainDataCollectionTypeMapper;

    /**
     * 列表
     *
     */
    @Override
    public Result list(MainDataCollectionTypeListReq req, TokenUser currentUser) {
        QueryWrapper<MainDataCollectionType> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MainDataCollectionType::getIsDeleted, IsDeletedCode.NO.getCode());
        wrapper.lambda().eq(MainDataCollectionType::getStatus, ISCode.NO.getCode());
        IPage<MainDataCollectionType> pageParam = new Page<>(req.getPageNo(), req.getPageSize());
        IPage<MainDataCollectionType> pageResult = mainDataCollectionTypeMapper.selectPage(pageParam, wrapper);
        List<MainDataCollectionType> records = pageResult.getRecords();
        List<MainDataCollectionTypeDTO> dtoList = EntityUtil.copyList(records, MainDataCollectionTypeDTO.class);
        PageResponse<MainDataCollectionTypeDTO> pageResponse = new PageResponse<>(dtoList,
                pageResult.getCurrent() == 1,
                pageResult.getTotal() <= (pageResult.getCurrent() * pageResult.getSize()),
                pageResult.getTotal(), pageResult.getCurrent(),
                pageResult.getSize());
        return Result.success(pageResponse);
    }

    /**
     * 详情
     *
     */
    @Override
    public Result detail(MainDataCollectionTypeDetailReq req, TokenUser currentUser) {
        QueryWrapper<MainDataCollectionType> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MainDataCollectionType::getId, req.getId());
        wrapper.lambda().eq(MainDataCollectionType::getIsDeleted, IsDeletedCode.NO.getCode());
        wrapper.lambda().eq(MainDataCollectionType::getStatus, ISCode.NO.getCode());
        MainDataCollectionType mainDataCollectionType = mainDataCollectionTypeMapper.selectOne(wrapper);
        MainDataCollectionTypeDTO dto = EntityUtil.copy(mainDataCollectionType, MainDataCollectionTypeDTO.class);
        return Result.success(dto);
    }
}
